﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections;

namespace Eugenhome.Atrium.Rss
{
    /// <summary>
    /// 
    /// </summary>
    public class RssCollection : CollectionBase, IEnumerable
    {
        /// <summary></summary>
        private List<Rss> _items = new List<Rss>();

        /// <summary></summary>
        public List<string> Errors { get { return _pageContent != null ?_pageContent.Errors:new List<string>(); } }

        /// <summary></summary>
        public new int Count { get { return _items.Count; } }

        /// <summary></summary>
        public bool Loaded { get { return _pageContent!=null && _pageContent.Loaded; } }

        /// <summary></summary>
        public bool Responsed { get { return _pageContent != null && _pageContent.Responsed; } }

        /// <summary></summary>
        private string _proxyAddress = "";

        /// <summary></summary>
        private PageContent _pageContent;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="proxyAddress"></param>
        public RssCollection(string proxyAddress)
        {
            _proxyAddress = proxyAddress;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public Rss this[int i] { get { return _items[i]; } }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            _items.Clear();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="databind"></param>
        /// <returns></returns>
        public bool DataBind(string url, bool databind)
        {
            if (_pageContent != null)
            {
                _pageContent.Loaded = databind;
                _pageContent.Responsed = false;
            }

            if (databind)
            {
                _pageContent = new PageContent(url, _proxyAddress);

                //string content = RequestUrlContent(url);

                if (Loaded)
                {
                    XmlDocument doc = new XmlDocument();
                    try
                    {
                        doc.LoadXml(_pageContent.Content);
                        foreach (XmlNode node in doc.GetElementsByTagName("item"))
                        {
                            _items.Add(new Rss(node));
                        }
                    }
                    catch (Exception ex)
                    {
                        _pageContent.Errors.Add("XmlDocument: " + ex.Message);
                        _pageContent.Loaded = false;
                    }
                }
            }

            return Loaded;
        }

        //public string RequestUrlContent(string url)
        //{
        //    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        //    if (!_proxyAddress.Trim().Equals(string.Empty))
        //    {
        //        req.Proxy = new WebProxy(_proxyAddress);
        //    }

        //    string content = "";

        //    try
        //    {
        //        //req.Timeout = 300; 
        //        //req.ReadWriteTimeout = 300;
        //        //req.KeepAlive = false;

        //        HttpWebResponse res = (HttpWebResponse)req.GetResponse();

        //        Responsed = true;

        //        StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding(res.CharacterSet));

        //        content = sr.ReadToEnd();
        //        sr.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        Errors.Add("HttpWebResponse: " + ex.Message);
        //        Loaded = false;
        //    }
        //    return content;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hashs"></param>
        public void Delete(List<int> hashs)
        {
            foreach (Rss rssitem in _items.ToList())
            {
                if (hashs.Contains(rssitem.Hashcode))
                {
                    _items.Remove(rssitem);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastReaded"></param>
        public void DeleteOld(DateTime lastReaded)
        {
            foreach (Rss rssitem in _items.ToList())
            {
                if (rssitem.PubDate <= lastReaded)
                {
                    _items.Remove(rssitem);
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        new public System.Collections.IEnumerator GetEnumerator()
        {
            foreach (Rss value in this._items) //this does not work because _list is private
                yield return value;
        }
    }
}
